template.defaults.imports.HTMLEncode=tools.HTMLEncode;
template.defaults.imports.HTMLDecode=tools.HTMLDecode;
template.defaults.imports.SBEncode=tools.SBEncode;
template.defaults.imports.CONFIG=CONFIG;
template.defaults.imports.DateFormat=function(data,option){
    return new Date(data).Format(option)
};
template.defaults.imports.imageArr = function (img) {
    if(img){
        var arr = img.split("∮");
        $(arr).each(function (index,item) {
            arr[index] = CONFIG.IMAGE_RESOURCES_ADDRESS + item;
        });
        return arr;
    }else{
        return [];
    }
};
$.fn.renderDataList = function (option) {
    var _seft = this;
    var flag = true;
    var defaults = {
        api: 'getDataList',
        action: 'culturalCloudContentSuperiorServiceListV10',
        params:{
            menuId: tools.getUrlParam('menuId'),
            stairColumnId: tools.getUrlParam('stairColumnId'),
            pageIndex: 0,
            pageSize: 10
        },
        render:function (data) {
            var html='';
            console.log('length',data.datas.length)
            if(data.datas.length>0){
                console.log(_seft[0].id)
                html= template(_seft[0].id, data.datas);
                if(data.totalPage > 1){
                    o.loading(true);
                }else{
                    o.loading(false);
                }
            }else {
                o.loading(false);
            }
            if (o.params.pageIndex === 0 ) {
                _seft.siblings().remove();
                _seft.parent().append(html);
            } else {
                _seft.parent().append(html);
            }
            _seft.siblings().addClass('fadeInUp');
            o.params.pageIndex++;
            flag = true;
        },
        loading : function (isMore) {
            $(".k-square-holder2").remove();
            var str;
            if(isMore){
                str = '<div class="k-square-holder2">' +
                            '<div>' +
                                '<div class="k-square4 k-square4a"></div>' +
                                '<div class="k-square4 k-square4b"></div>' +
                                '<div class="k-square4 k-square4c"></div>' +
                                '<div class="k-square4 k-square4d"></div>' +
                            '</div>' +
                            '<p>正在加载</p>' +
                        '</div>';
            }else{
                str = '<div class="k-square-holder2">' +
                            '<p>没有更多内容</p>' +
                        '</div>';
            }
            _seft.parent().after(str);
        }
    };
    var o=$.extend(true,defaults, option);



    var _init = function () {
        if(!$("#pullUpLoad").length){
            $("<link>")
                .attr({
                    id: 'pullUpLoad',
                    rel: "stylesheet",
                    type: "text/css",
                    href: "../css/loading.css"
                })
                .appendTo("head");
        }
        API.getDataList(o.action,o.params,o.render)
    };
    var _event = function () {
        $(window).off('scroll');
        $(window).scroll(function () {
            if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
                if(flag){
                    flag = false;
                    API.getDataList(o.action,o.params,o.render)
                }
            }
        });
    };
    _init();
    _event();
};
